package com.ucode.cgms.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ucode.cgms.mapper.CgmsCompanionCommentMapper;
import com.ucode.cgms.service.CgmsCompanionCommentService;
import com.ucode.model.cgms.CgmsCompanionComment;
import com.ucode.model.cgms.convert.CgmsCompanionCommentConvert;
import com.ucode.model.cgms.vo.CgmsCompanionCommentVO;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.util.IdentifyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 陪玩评论服务实现类
 * 
 * @Author: system
 * @Date: 2025/01/15 10:00
 * @Version: 1.0
 */
@Service
public class CgmsCompanionCommentServiceImpl implements CgmsCompanionCommentService {
    
    @Resource
    private CgmsCompanionCommentMapper cgmsCompanionCommentMapper;
    
    @Override
    public CgmsCompanionCommentMapper initMapper() {
        return cgmsCompanionCommentMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(CgmsCompanionComment cgmsCompanionComment) {
        if(ObjUtil.isNull(cgmsCompanionComment.getId()))
            cgmsCompanionComment.setId(IdentifyUtils.getDefaultSnowflakeId());
        cgmsCompanionCommentMapper.insert(cgmsCompanionComment);
        return cgmsCompanionComment.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateVisible(Long id, Boolean visible) {
        // 校验评论是否存在
        this.validate(id);
        // 更新可见性
        CgmsCompanionComment comment = new CgmsCompanionComment();
        comment.setId(id);
        comment.setVisible(visible);
        return this.update(comment) > 0;
    }
    
    @Override
    public CgmsCompanionComment getByOrderId(Long orderId) {
        if (ObjectUtil.isNull(orderId)) {
            return null;
        }
        
        // 根据订单ID查询评论
        return cgmsCompanionCommentMapper.selectByOrderId(orderId);
    }
    
    @Override
    public List<CgmsCompanionComment> getByCompanionUserId(Long companionUserId) {
        if (ObjectUtil.isNull(companionUserId)) {
            return null;
        }
        return cgmsCompanionCommentMapper.selectByCompanionUserId(companionUserId);
    }
    
    @Override
    public Paginator<CgmsCompanionCommentVO> pageListByCompanionUserId(Long companionUserId, Integer pageNum, Integer pageSize) {
        // 创建查询条件
        Map<String, Object> condition = new HashMap<>();
        condition.put("companionUserId", companionUserId);
        condition.put("visible", true); // 只查询可见的评论
        condition.put("deltag", false); // 只查询未删除的评论
        
        // 查询分页数据
        Paginator<CgmsCompanionComment> paginator = this.queryPageForList(condition, pageSize, pageNum);
        
        // 转换为VO
        return CgmsCompanionCommentConvert.INSTANCE.convertPage(paginator);
    }
} 